*Set up definition work working class individuals using adminstrative data
clear
cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Do Files\Worker Rep\"
		
odbc load, exec("select * from Individ_2001") dsn("P0934") clear
gen year=2001
save kolfu_base, replace
forvalues year=2002/2015{	
	odbc load, exec("select * from Individ_`year'") dsn("P0934") clear
	gen year = `year'
	append using kolfu_base, force
	save kolfu_base, replace
}
ren P0934_Lopnr_PersonNr p_id
gen pbb = 44500 if year == 2015
replace pbb = 44400 if year == 2014
replace pbb = 44400 if year == 2013
replace pbb = 44000 if year == 2012
replace	pbb	= 42800	if 	year ==	2011
replace	pbb	= 42400	if 	year ==	2010
replace	pbb	= 42800	if 	year ==	2009
replace	pbb	= 41000	if 	year ==	2008
replace	pbb	= 40300	if 	year ==	2007
replace	pbb	= 39700	if 	year ==	2006
replace	pbb	= 39400	if 	year ==	2005
replace	pbb	= 39300	if 	year ==	2004
replace	pbb	= 38600	if 	year ==	2003
replace	pbb	= 37900	if 	year ==	2002
replace	pbb	= 36900	if 	year ==	2001
replace pbb=pbb/100 
gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"
ren Ssyk4_2012 ssyk4_2012 
destring ssyk4_2012 Ssyk4, force replace
joinby ssyk4_2012 using ssyk4_2012_to_ssyk96.dta , unmatched(master)
drop _merge
replace Ssyk4= ssyk96kod if year>=2014
gen ssyk96 =int(Ssyk4/10)
joinby ssyk96 using "ssyk96_esec_egp", unmatched(master)
drop _merge
destring AstSNI*, force replace
joinby AstSNI2007 using "sni2007_to_sni2002" , unmatched(master)
replace AstSNI2002 =AstSNI2002_con if AstSNI2002==. & AstSNI2002_con!=. 
drop _merge  AstSNI2002_con
joinby AstSNI92 using "sni92_to_sni2002" , unmatched(master)
replace AstSNI2002 =AstSNI2002_con if AstSNI2002==. & AstSNI2002_con!=.
drop _merge  AstSNI2002_con
replace AstSNI2002=0 if AstSNI2007==0 |AstSNI92==0
recode AstSNI2002 (150/379=1)(.=.)(else=0)					      , gen(manufacturing)
recode AstSNI2002 (500/749 900/909 920/939=1)(.=.)(else=0)		  , gen(priv_service)
recode AstSNI2002 (750/759 800/809 850/859=1)(.=.)(else=0)		  , gen(pub_service)
gen elseind = 0 if AstSNI2002!=.
replace elseind = 1 if manufacturing==0 & priv_service==0 & pub_service==0
gen industry = manufacturing*1 + priv_service*2 + pub_service*3+elseind*4
drop manufacturing- elseind 
ren ssyk96 ssyk3
mmerge ssyk3 industry using "isco_sei_key_2", type(n:1) unmatched(master) missing(nomatch) umatch(isco3 industry)
ren modsei sei

replace sei=95 if Stud>=1 & Stud!=.
replace sei=. if Alder>=65
replace sei =. if politician==1
sort p_id year
forvalues n=1/15{
	replace sei=sei[_n-`n'] if sei==. & p_id ==p_id[_n-`n']
}
keep if year==2015
gen worker=sei<30 if sei!=.
gen  stud = sei==95 if sei!=.
gen pens=sei==99 if sei!=.
drop _merge
save workers, replace

* Kolfu political ideology, economic policy preferences and qualification measures

cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\"
use "Kolfu answers clean.dta", clear

* Keep only municipal politicians in national parties
drop if Q01=="Landstings-/regionfullmäktige"
drop if v09_Parti9=="Övriga partier"
gen valar=2014

*Add adminstrative data on working class bacground
cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Do Files\Worker Rep\"
joinby p_id using workers,  unmatched(master) 
**Add data on politicians
drop _merge
joinby p_id valar using "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\ko_politicians_82_14", unmatched(master) 
drop _merge

joinby p_id valar using "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\arv_ko_07_15_clean.dta", unmatched(master)
	cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Do Files\Worker Rep\"
drop _merge
joinby p_id using uppdrag,  unmatched(master) 
drop _merge
gen muni=llkk
gen party= partikod 
egen muni_party=group(muni party)
tab v06_Alder
destring v06_Alder, replace force
gen a1 = v06_Alder<30
gen a2 = v06_Alder>29 & v06_Alder<40
gen a3 = v06_Alder>39 & v06_Alder<50
gen a4 = v06_Alder>49 & v06_Alder<65
gen a5 = v06_Alder>64 & v06_Alder!=.
gen woman = 1 if Kon=="Kvinna"
replace woman = 0 if Kon=="Man"
gen parti = 1 if v09_Parti9 =="Centerpartiet"
replace parti = 2 if v09_Parti9 =="Kristdemokraterna"
replace parti = 3 if v09_Parti9 =="Liberalerna"
replace parti = 4 if v09_Parti9 =="Miljöpartiet"
replace parti = 5 if v09_Parti9 =="Moderaterna"
replace parti = 6 if v09_Parti9 =="Socialdemokraterna"
replace parti = 7 if v09_Parti9 =="Sverige demokraterna"
replace parti = 8 if v09_Parti9 =="Vänsterpartiet"


gen a4a = 1 if v06_Alder<30
replace a4a = 2 if v06_Alder>29 & v06_Alder<49
replace a4a = 3 if v06_Alder>49 & v06_Alder<65
replace a4a = 4 if v06_Alder>64 & v06_Alder!=.

replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999

gen edu3=.
replace edu3= 1 if Sun2000niva>=100 & Sun2000niva<200
replace edu3= 1 if Sun2000niva>=200 & Sun2000niva<300
replace edu3= 1 if Sun2000niva>=300 & Sun2000niva<330
replace edu3= 2 if Sun2000niva>=330 & Sun2000niva<400
replace edu3= 3 if Sun2000niva>=410& Sun2000niva<530
replace edu3= 3 if Sun2000niva>=530 & Sun2000niva<600
replace edu3= 3 if Sun2000niva>=600 & Sun2000niva<=999



gen list_leader=1 if nrinom_hl==1
replace list_leader=0 if nrinom_hl!=. & list_leader==. 
gen led = 1
replace led = 0 if list_leader==1
* class
gen class = sei
replace class = 11 if class==21
tab class
recode class(11=1)(36=2)(46=3)(56=4)(79=5)(89=6)(95=7)
replace class=. if class==7

* Left-right ideology
tab Q13
destring Q13, replace force
egen s_leftright = std(Q13)


* Reduce size of the public sector
tab q17_11
foreach var in q17_11 q17_12 {
	replace `var' = "1" if `var'=="Mycket dåligt"
	replace `var' = "2" if `var'=="Ganska dåligt"
	replace `var' = "3" if `var'=="Varken bra eller dåligt"
	replace `var' = "4" if `var'=="Ganska bra"
	replace `var' = "5" if `var'=="Mycket bra"
	destring `var', replace force
	}

ren q17_11 cutpublic 
egen s_cutpublic = std(cutpublic)


foreach var of varlist q52_1-q52_12{
	gen   `var'_num=1 if `var'=="Håller inte alls med"
	replace  `var'_num=2 if `var'=="Håller inte med"
	replace   `var'_num=3 if `var'=="Neutral"
	replace  `var'_num=4 if `var'=="Håller med"
	replace   `var'_num=5 if `var'=="Håller med helt"

}


	egen sumperry = rowtotal(q52_1_num-q52_12_num)
	egen nonmissperry = rownonmiss(q52_1_num-q52_12_num)
	replace sumperry=. if nonmissperry<12
	gen publicservice = sumperry/nonmissperry 
	replace publicservice = publicservice/4
	

		foreach var of varlist q51_1-q51_10{
	gen   `var'_num=1 if `var'=="Håller inte alls med"
	replace  `var'_num=2 if `var'=="Håller inte med"
	replace   `var'_num=3 if `var'=="Neutral"
	replace  `var'_num=4 if `var'=="Håller med"
	replace   `var'_num=5 if `var'=="Håller med helt"
}
	foreach var in q51_5_num q51_2_num q51_4_num q51_7_num q51_8_num q51_10_num {
		
		recode `var' (0=4) (1=3) (3=1) (4=0)
	}
	
  	egen honesty_hum = rowmean(q51_1_num-q51_10_num)
	replace honesty_hum = honesty_hum/4
	hist honesty_hum

	egen std_publicservice=std(publicservice)
	egen std_honesty_hum=std(honesty_hum)


foreach var in q12a_1 q12a_2 q12a_3 q12b_1 q12b_2 q12b_3 q12b_4 q12b_5 q12c_1 q12c_2 {
	destring `var', force replace
	replace `var' = . if `var'>100 & `var'!=.
}

gen lnhrs_position = log(q12a_2+0.1)
gen hrs_politics = q12a_1
gen hrs_position_only = q12a_2
gen hrs_outside_position = q12a_3
gen pol_meetings = q12b_1
gen party_meetings = q12b_2
gen prep_meetings = q12b_3
gen bur_meetings = q12b_4
gen voteretc_meetings = q12b_5
gen nr_camp = q12c_1
gen nr_camp_part = q12c_2
replace arbetstid =0 if arbetstid==.

gen camp_sh=q12c_2/q12c_1
egen std_camp_sh=std(camp_sh)
foreach var in hrs_politics  hrs_position_only{
	areg `var', abs(arbetstid)
	predict `var'_res, resid
	egen std_`var'_res=std(`var'_res)
}


